<?php

/*
 * This class is used for as Model for Role Access table 
 */

class Model_Role extends Model_Base {

    protected $_name = 'tbl_role_access';
    protected $_primaryKey = 'role_id';

    //fetch all contractor
    public function getcontractorArray($company_id) {
        $contrctorModel = new Model_Contractor();
        $where = "cont_isadmin!=1 and con.com_id = " . $company_id;
        $contractorData = $contrctorModel->fetchContractor($where);
        $finalArray[''] = "Select Contractor";
        foreach ($contractorData as $com) {
            $finalArray[$com['lgn_id']] = ucfirst($com['cont_firstname']) . " " . ucfirst($com['cont_lastname']);
        }
        return $finalArray;
    }
    
    
    //fetch all customer
    public function getcustomerArray($lang_id , $company_id) {
        $customerModel = new Model_Customer();
        $where = " cust_isadmin!=1 and cust.com_id = " . $company_id;
        $customerData = $customerModel->fetchCustomer($lang_id,$where);
        $finalArray[''] = "Select Customer";
        foreach ($customerData as $com) {
            $finalArray[$com['lgn_id']] = ucfirst($com['cust_firstname']) . " " . ucfirst($com['cust_lastname']);
        }
        return $finalArray;
    }

    public function getRoletypeArray($role_type) {
        if ($role_type == 'SuperAdmin') {
            return array(
                '' => '-- Select Type --',
                '1' => 'Admin',
                '2' => 'Contractor',
                '3' => 'Customer'
            );
        } else {
            return array(
                '' => '-- Select Type --',
                '2' => 'Constractor',
                '3' => 'Customer'
            );
        }
    }

    public function getRoles() {
        $roles = Array('1' => 'ADMIN', '2' => 'CONTRACTOR', '3' => 'CUSTOMER');
        return $roles;
    }

    public function getStatusArray() {
        return array(
            '' => '-- Select Status --',
            '1' => 'Active',
            '0' => 'Inactive'
        );
    }

    public function fetchEntryByloginId($id, $where = "", $group = "") {

        $select = $this->select()
                ->from(array('role_access' => 'tbl_role_access'))
                ->where('lgn_id = ?', $id);
        if ($where != '') {
            $select->where($where);
        }
        if ($group != '') {
            $select->group($group);
        }
        # echo $select;
        // see reasoning in fetchEntries() as to why we return only an array
        $data = $this->fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     * Fetch an individual entry with the role type
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryById($id, $where) {
        $select = $this->select()
                ->from(array('role_access' => 'tbl_role_access'))
                ->where('role_type = ?', $id);
        if ($where != '') {
            $select->where($where);
        }
        #echo $select;exit;
        // see reasoning in fetchEntries() as to why we return only an array
        $data = $this->fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    //get Resources data
    public function module_function() {

        $resourcesmodel = new Model_Resources();
        $resources_data = $resourcesmodel->fetchResourcesAccess();

        if (!empty($resources_data)) {
            return $resources_data;
        } else {
            return array();
        }
    }

    //get Resources data by admin
    public function getResources() {
        $resourcesmodel = new Model_Resources();
        $resources_data = $resourcesmodel->fetchResourcespluginAccess("", "", "", "order_no", "ASC", "res_controller_name");
        if (count($resources_data) > 0) {
            return $resources_data;
        }
        return array();
    }

    public function getGlobalResources() {
        $resourcesmodel = new Model_Resources();
        $resources_data = $resourcesmodel->fetchResglobalAccess("", "", "", "order_no", "ASC", "");
        if (count($resources_data) > 0) {
            return $resources_data;
        }
        return array();
    }

    //get Resources data by admin
    public function getResourceadminarray($roletype = "", $com_id = "") {
        $resourcesmodel = new Model_Resources();
        $resources_data = $resourcesmodel->fetchResourcesAccess("", "", "", "order_no", "ASC", "res_module_name");
        $count_data = count($resources_data);
        // $main_array=array();
        for ($i = 0; $i < $count_data; $i++) {
            $main_array[$i]['module_name'] = $resources_data[$i]['res_module_name'];
            $main_array[$i]['controller_name'] = $resources_data[$i]['res_controller_name'];
            $where = 'res_module_name="' . $resources_data[$i]['res_module_name'] . '"';
            $resources_actiondata = $resourcesmodel->fetchResourcesAccess($where, "", "", "order_no", "ASC", "");
            $j = 0;
            foreach ($resources_actiondata as $key => $value) {
                $main_array[$i]['action'][$j]['chkbox_value'] = $value['res_action_name'];
                $main_array[$i]['action'][$j]['name'] = $value['res_display_name'];
                $main_array[$i]['action'][$j]['checkbox_checked'] = 'false';
                $j++;
            }
        }

        return $main_array;
    }

    //get Resources data by contractor
    public function getResourcecontractorarray($roletype, $com_id) {
        $where_role = "com_id=" . $com_id;
        $group_by = "menu_module_name";
        $resourcesmodel = new Model_Resources();
        $roleaccess_data = $resourcesmodel->fetchResourcesRoleAccess($where_role, "", "0", 'order_no', 'ASC', $group_by);
        /*echo '<pre>';
        print_R($roleaccess_data);
        exit;*/
        
        $main_array = array();
        $i = 0;
        foreach ($roleaccess_data as $rol_key => $role_value) {
            //array contain all controller without roll controller
            //Roll controller not allow for subcontractor
            if ($role_value['res_controller_name'] != 'role') {
                $main_array[$i]['module_name'] = $role_value['menu_module_name'];
                $main_array[$i]['controller_name'] = $role_value['res_controller_name'];
                $where_action = "com_id='" . $com_id . "' and menu_module_name = '" . $role_value['menu_module_name'] . "'";
                $resources_actiondata = $resourcesmodel->fetchResourcesRoleAccess($where_action);
                $j = 0;
               
                foreach ($resources_actiondata as $key => $value) {
                    
                    $main_array[$i]['action'][$j]['chkbox_value'] = $value['res_action_name'];
                    $main_array[$i]['action'][$j]['name'] = $value['res_display_name'];
                    $main_array[$i]['action'][$j]['checkbox_checked'] = 'false';
                    $j++;
                }
            }

            $i++;
        }

        return $main_array;
    }

    //get Resources data by modulename
    public function getResourcearray($roletype, $com_id) {

        if ($roletype == 'SuperContractor') {
            $contrctorAccess = $this->getResourcecontractorarray($roletype, $com_id);
            return $contrctorAccess;
        }else if ($roletype == 'SuperCustomer') {
            $customerAccess = $this->getResourcecontractorarray($roletype, $com_id);
            return $customerAccess;
        } else {
            $adminAccess = $this->getResourceadminarray($roletype, $com_id);
            return $adminAccess;
        }
    }

    /**
     *  This function is used to get roles
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchRoleAccess($where = "", $limit = "", $offset = "0", $order = 'role_id', $sort = 'ASC', $group = '') {
        $select = $this->select()
                ->from(array('role_access' => 'tbl_role_access'));
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        if ($group != "") {
            $select->group($group);
        }
        $select->order($order . " " . $sort);
        #echo $select;exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get total orders
     * @return int
     */
    public function getRoleAccessCount($where) {
        $select = $this->select()
                ->from(array('role_access' => 'tbl_role_access'), Array('cnt' => 'count(*)'));
        if ($where != '') {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }

    //fetch company  contractor
    public function fetch_company_contractor($company_id) {
        $contrctorModel = new Model_Contractor();
        $where = "con.com_id = " . $company_id;
        $contractorData = $contrctorModel->fetchContractor($where);

        if (!empty($contractorData)) {
            return $contractorData;
        } else {
            return array();
        }
    }

    //fetch all contractor
    public function fetch_company_customer($company_id) {
        $customerModel = new Model_Customer();
        $where = "cust.com_id = " . $company_id;
        $customerData = $customerModel->fetchCustomer($where);
        if (!empty($customerData)) {
            return $customerData;
        } else {
            return array();
        }
    }

    public function ajax_roletype($role_type, $selected_id = "") {

        //For admin
        if ($role_type == 1) {
            $adminModel = new Model_Admin();
            // check for superadmin
            $where = "adm_issuperadmin != 1";

            $adminData = $adminModel->fetchAdmin($where);
            $listString = "<div class='ui-select'>
                             <select name='admin_name' id='admin_company_name' >
                             <option value=''>Select Admin</option>";
            foreach ($adminData as $key => $value) {
                if ($selected_id == $value['lgn_id']) {
                    $listString.="<option value='" . $value['lgn_id'] . "' selected='selected'>" . ucfirst($value['adm_firstname']) . " " . ucfirst($value['adm_lastname']) . "</option>";
                } else {
                    $listString.="<option value='" . $value['lgn_id'] . "'>" . ucfirst($value['adm_firstname']) . " " . ucfirst($value['adm_lastname']) . "</option>";
                }
            }
            $listString.="</select></div>";
        }
        //For Contrator
        if ($role_type == 2) {

            $companyModel = new Model_Company();
            $where = "com_type=1";
            $companyData = $companyModel->fetchCompany($where);

            $listString = "<div class='ui-select'>
                             <select name='company_name' id='admin_company_name'>
                             <option value=''>Select Company</option>";
            foreach ($companyData as $key => $value) {
                if ($selected_id == $value['com_id']) {
                    $listString.="<option value='" . $value['com_id'] . "' selected='selected'>" . ucfirst($value['com_name']) . "</option>";
                } else {
                    $listString.="<option value='" . $value['com_id'] . "'>" . ucfirst($value['com_name']) . "</option>";
                }
            }
            $listString.="</select></div>";
        }
        //For Customer
        if ($role_type == 3) {
            $companyModel = new Model_Company();
            $where = "com_type=2";
            $companyData = $companyModel->fetchCompany($where);
            $listString = "<div class='ui-select'>
                             <select name='company_name' id='admin_company_name'>
                             <option value=''>Select Company</option>";
            foreach ($companyData as $key => $value) {
                if ($selected_id == $value['com_id']) {
                    $listString.="<option value='" . $value['com_id'] . "' selected='selected'>" . ucfirst($value['com_name']) . "</option>";
                } else {
                    $listString.="<option value='" . $value['com_id'] . "'>" . ucfirst($value['com_name']) . "</option>";
                }
            }
            $listString.="</select></div>";
        }

        return $listString;
    }

}
